package com.etime.shycourse.controller.wx.live;

import com.alibaba.fastjson.JSONObject;
import com.etime.shy.common.BaseMessage;
import com.etime.shycourse.config.Tools;
import com.etime.shycourse.controller.wx.rss.WXRSSService;
import com.etime.shycourse.dto.CurrentUser;
import com.etime.shycourse.pojo.LiveRecord;
import com.etime.shycourse.services.interfaces.LiveRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 直播相关数据接口
 */
@Api(tags = "微信-直播-相关数据接口")
@RestController
@RequestMapping("/wx/live")
public class LiveController {

    @Autowired
    private WXRSSService wxrssService;
    @Autowired
    private LiveRecordService liveRecordService;
    @Autowired
    private Tools tools;

    @ApiOperation("获取直播列表")
    @PostMapping("/list")
    public Object selectLiveList() {
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=" + wxrssService.getAccessToken();
        Map<String, Object> map = new HashMap<>();
        map.put("start", 0);
        map.put("limit", 20);
        Map<String, Object> rtn = restTemplate.postForObject(url, JSONObject.toJSONString(map), Map.class);
        return rtn;
    }

    @ApiOperation("插入直播课程参与记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "liverecordid", value = "主键"),
            @ApiImplicitParam(name = "livetitle", value = "直播题目"),
            @ApiImplicitParam(name = "liveroomid", value = "直播间id"),
            @ApiImplicitParam(name = "livename", value = "主播姓名"),
            @ApiImplicitParam(name = "liveuserid", value = "参与人id"),
            @ApiImplicitParam(name = "liveusername", value = "参与人姓名"),
            @ApiImplicitParam(name = "livetime", value = "参与时间"),
            @ApiImplicitParam(name = "liveorreview", value = "直播or回放，传入直播或者回放"),
            @ApiImplicitParam(name = "liveinorout", value = "1-进入；2-退出"),
            @ApiImplicitParam(name = "livefield1", value = "备用字段1"),
            @ApiImplicitParam(name = "livefield2", value = "备用字段2"),
            @ApiImplicitParam(name = "livefield3", value = "备用字段3")
    })
    @PostMapping("/record/insert")
    public BaseMessage insertLiveRecode(@RequestBody LiveRecord liveRecord, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage();
        //判断用户是否登录
        String token = request.getHeader("token");
        //String token = "1679091c5a880faf6fb5e6087eb1b2dc";
        CurrentUser currentUser = tools.getCurrentUserFromRedis(token);
//        if (currentUser == null) {
//            msg.setCode(-2);
//            msg.setMsg("未获取到用户信息；可能登录超时或者非法访问。");
//        }else {
        if (currentUser != null) {
            liveRecord.setLiveuserid(currentUser.getUser().getUserid());
            liveRecord.setLiveusername(currentUser.getUser().getUsertruename() + "(" +
                    currentUser.getUser().getNickname() + ")");
        }
        liveRecord.setLivetime(new Date());
        int rtn = liveRecordService.insertLiveRecord(liveRecord);
        if (rtn > 0) {
            msg.setCode(1);
            msg.setMsg("保存记录成功");
        } else {
            msg.setCode(0);
            msg.setMsg("保存记录失败");
        }
//        }
        return msg;
    }
}
